﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18	
 */

var keyValue = request('keyValue');
var year = request("year");
var userdefault = {};

var bootstrap = function ($, learun) {
    "use strict";

    var jfGrid = null;

    // 保存数据
    var acceptClick = function (type) {// 0保存并新增 1保存
        if (!$('.lr-layout-wrap').lrValidform()) {
            return false;
        }
        var productData = [];
        var productDataTmp = $('#productgird').jfGridGet('rowdatas');

        if (productDataTmp.length == 0) {
            learun.alert.error('表单信息不全,请检查！');
            return false;
        }

        for (var i = 0, l = productDataTmp.length; i < l; i++) {
            if (!!productDataTmp[i]['departmentId']) {
                if (!productDataTmp[i].money) {
                    learun.alert.error('表单信息不全,请检查！');
                    return;
                }
                productDataTmp[i]['year'] = year;
                productDataTmp[i]['projectId'] = keyValue;
                productDataTmp[i]['sort'] = i + 1;
                productData.push(productDataTmp[i]);
            }
        }

        var postData = productData;

        learun.layerConfirm('注：您确认要执行分配吗？', function (res, index) {
            if (res) {
                $.lrSaveForm(config.base_server() + 'pbdbudgetadjust1s/saveOrUpdate?access_token=' + config.getToken().access_token + '&keyValue=' + keyValue, JSON.stringify(postData), function (res) {
                    if (res.resp_code == 200) {
                        learun.frameTab.close('form_allocation1');
                    }
                });

                top.layer.close(index); //再执行关闭
            }
        });
    };

    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['userinfo']);
            page.bind();
            page.initData();
        },
        bind: function () {
            // 优化滚动条
            $('.lr-layout-wrap').lrscroll();

            // 订单产品信息
            jfGrid = $('#productgird').jfGrid({
                headData: [
                    {
                        label: "执行部门", name: "departmentName", width: 200, align: "center",
                        edit: {
                            type: 'layer',
                            init: function (data, $edit, rownum) {// 在点击单元格的时候触发，可以用来初始化输入控件，行数据和控件对象
                                var rowCompanyId = data.companyId;
                                learun.layerForm({
                                    id: 'form_department',
                                    title: '执行部门',
                                    url: config.webportal_server + 'pages/system/department/selectForm.html?btnIndex=0' + '&access_token=' + config.getToken().access_token,
                                    width: 660,
                                    height: 600,
                                    callBack: function (id) {
                                        var res = top[id].acceptClick(function (data2) {
                                            data.departmentId = data2.value;
                                            data.departmentName = data2.text;
                                            $('#productgird').jfGridSet('updateRow', rownum);
                                        });

                                        top.lrGirdLayerEdit = null;
                                        return res;
                                    }
                                });
                            },
                            change: function (row, rownum, selectdata) {// 行数据和行号,弹层选择行的数据，如果是自定义实现弹窗方式则该方法无效
                            },
                        }
                    },
                    {
                        label: '金额', name: 'money', width: 250, align: 'center', statistics: true,
                        edit: {
                            type: 'number'
                        }
                    },
                    {
                        label: '部门id', name: 'departmentId', width: 1, align: 'center', hidden: true
                    }
                ],
                isEdit: true,
                height: 600,
                isMultiselect: true
            });

            // 保存数据
            $('#savaAndAdd').on('click', function () {
                acceptClick(0);
            });
            $('#save').on('click', function () {
                acceptClick(1);
            });

            //初始化默认值
            var loginInfo = learun.clientdata.get(['userinfo']);
            $("#createUsername").val(loginInfo.user.realName);
            $("#createTime").val(new Date().format("yyyy-MM-dd"));
        },
        initData: function () {
            if (!!keyValue) {
                $.lrSetForm(config.base_server() + 'pbdbudgetadjust1s/findByProjectId?projectId=' + keyValue + '&year=' + year + '&access_token=' + config.getToken().access_token, function (data) {
                    $('#productgird').jfGridSet('refreshdata', data);
                    if (data.length == 0) {
                        $('#productgird').jfGridSet('addRow', {});
                    }
                });
            } else {
                $('#productgird').jfGridSet('addRow', {});
            }
        }
    };

    page.init();
}